package mqtt5;

import lombok.Data;
import org.eclipse.paho.mqttv5.client.MqttConnectionOptions;

/**
 * @Title: Emqx5Config
 * @Package: cn.mis.util.mqtt5
 * @Description: mqttv5客户端相关配置信息
 * @Author: ChenLi
 * @Date: 2023/2/16 - 13:24
 */
@Data
public class Emqx5Config {
    private String clientName;
    /*支持多个节点，在断开后进行自动切换*/
    private String brokerUrl;

    private String clientId;

    private String username;

    private String password;

    /*初始化订阅主题*/
    private String eventReportTopic;
    private Boolean isCleanSession = true;
    private int reconnectMillisecond = 3000;
    private Integer connectionTimeout;
    private Integer keepAliveInterval;
    private Integer receiveMaximum;
    private Long maximumPacketSize;
    private String callback;

    private MqttConnectionOptions options;

    public void init() {
        options = new MqttConnectionOptions();
        //断线重连
        options.setAutomaticReconnect(true);
        options.setUserName(username);
        options.setPassword(password.getBytes());

        options.setCleanStart(isCleanSession);

        // 设置超时时间 单位为秒
        options.setConnectionTimeout(connectionTimeout);
        // 设置会话心跳时间 单位为秒 服务器会每隔1.5*10秒的时间向客户端发送个消息判断客户端是否在线，但这个方法并没有重连的机制
        options.setKeepAliveInterval(keepAliveInterval);

        options.setReceiveMaximum(receiveMaximum);
        options.setMaximumPacketSize(maximumPacketSize);

    }
}
